package com.xhwl.logistics.strategy.impl;

import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarportBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.util.AboutDateUtil;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractVehicleParkingLotHandlerImpl implements IVehicleParkingLotHandler {

    protected abstract List<VehicleParkingLotSecondBO> queryParkingLots(Integer projectId, String parkingLotName);

    protected abstract VehicleParkingLotSecondBO queryParkingLot(Integer projectId);

    protected abstract VehicleCarportBO getVehicleCarportBOFromHttp(VehicleIntegratedConfigBO vehicleIntegratedConfigBO);

    protected abstract Integer statisticsEnterCount(Integer projectId, String parkId, Date ingestionDate);

    protected abstract Integer statisticsGoOutCount(Integer projectId, String parkId, Date ingestionDate);

    protected abstract void pushDwsAccessRecordForHourDataToCentBi(Integer projectId, String parkingLotName,
                                                                   Integer totalForCount, Integer enterForCount, Integer goOutForCount,
                                                                   Date processDate, Date ingestionDate);

    protected abstract Integer statisticsTotalPaySum(Integer projectId, String parkId, Date ingestionDate);

    protected abstract void pushDwsPayRecordForHourDataToCentBi(Integer projectId, String parkingLotName,
                                                                Integer totalPay, Integer webChatPay, Integer aliPay, Integer onlinePay, Integer other,
                                                                Date processDate, Date ingestionDate);

    @Override
    public VehicleCarportBO queryCarport(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryVehicleCarportBO queryVehicleCarportBO) {
        Integer projectId = queryVehicleCarportBO.getProjectId();
        String parkingLotName = queryVehicleCarportBO.getParkingLotName();
        List<VehicleParkingLotSecondBO> vehicleParkingLotSecondBOList = queryParkingLots(projectId, parkingLotName);
        if (vehicleParkingLotSecondBOList.isEmpty()) {
            throw new BusinessException("通过项目id和车场名称找不到车场");
        }

        List<VehicleCarportBO> vehicleCarportBOList = vehicleParkingLotSecondBOList.stream()
                .map(vehicleParkingLotSecondBO -> getVehicleCarportBOFromHttp(vehicleIntegratedConfigBO))
                .collect(Collectors.toList());

        return vehicleCarportBOList.stream()
                .reduce(new VehicleCarportBO(0, 0), (x, y) -> new VehicleCarportBO(Integer.sum(x.getTotalNumber(), y.getTotalNumber()), Integer.sum(x.getFreeNumber(), y.getFreeNumber())));
    }

    @Override
    public void statisticsToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = queryParkingLot(projectId);
        String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
        String parkId = vehicleParkingLotSecondBO.getParkId();

        Date processDate = new Date();
        Date ingestionDate = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone), 1);
        Integer enterCount = statisticsEnterCount(projectId, parkId, ingestionDate);
        Integer goOutCount = statisticsGoOutCount(projectId, parkId, ingestionDate);
        Integer totalCount = enterCount + goOutCount;
        pushDwsAccessRecordForHourDataToCentBi(projectId, parkingLotName, totalCount, enterCount, goOutCount, processDate, ingestionDate);

        Integer totalPaySum = statisticsTotalPaySum(projectId, parkId, ingestionDate);
        pushDwsPayRecordForHourDataToCentBi(projectId, parkingLotName, totalPaySum, 0, 0, 0, 0, processDate, ingestionDate);
    }
}
